const {
  http
} = require("../../utils/apiutils");

// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    ptype: "",
    id: "",
    canIUseGetUserProfile:false
  },
  getPhoneNumber: function (e) {
    console.log("----------", e);
    if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
      wx.showToast({
        title: '取消授权',
        icon: "none",
      })
    } else if (e.detail.errMsg == "getPhoneNumber:ok") {
      var encryptedData = e.detail.encryptedData;
      var iv = e.detail.iv;
      var data = {
        "openid": getApp().globalData.openid,
        "encryptedData": encryptedData,
        "iv": iv
      }
      var that = this;
      wx.showLoading({
        title: '加载中...',
      })
      http.wechatBindPhone(data,
        function (res) {
          wx.hideLoading()
          console.log("------用户信息--", res);
          if (res.code == 0) {
            // that.getuser();
            wx.reLaunch({
              url: '/pages/index/index',
            })
          } else if (res.code == 1) {
            wx.showToast({
              title: '绑定微信成功，请绑定微信',
              icon: "none"
            })
            that.setData({
              type: 1
            })
          }
        },
        function (err) {
          wx.hideLoading()
        })
      console.log("----------", data);
    }

  },
  onclickBack: function () {
    // wx.navigateBack({
    //   delta: 1,
    // })
    wx.reLaunch({
      url: '/pages/tabBar/home/index',
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (e) => {
        console.log("----------", e);
        if (e.errMsg == "getUserProfile:fail auth deny") {
          wx.showToast({
            title: '取消授权',
            icon: "none",
          })
        } else if (e.errMsg == "getUserProfile:ok") {
          var encryptedData = e.encryptedData;
          var iv = e.iv;
          var data = {
            "openid": getApp().globalData.openid,
            "encryptedData": encryptedData,
            "iv": iv
          }
          var that = this;
          wx.showLoading({
            title: '加载中...',
          })
          http.wechatBindInfo(data,
            function (res) {
              wx.hideLoading()
              console.log("------用户信息--", res);
              if (res.code == 0) {
                that.wclogin();
              } else if (res.code == 2) {
                wx.showToast({
                  title: '绑定微信成功，请绑定手机号',
                  icon: "none"
                })
                that.setData({
                  type: 2
                })
              }
            },
            function (err) {
              wx.hideLoading()
            })
          console.log("----------", data);
        }
      }
    })
  },
  getuserinfo: function (e) {
    console.log("----------", e);
    if (e.detail.errMsg == "getUserInfo:fail auth deny") {
      wx.showToast({
        title: '取消授权',
        icon: "none",
      })
    } else if (e.detail.errMsg == "getUserInfo:ok") {
      var encryptedData = e.detail.encryptedData;
      var iv = e.detail.iv;
      var data = {
        "openid": getApp().globalData.openid,
        "encryptedData": encryptedData,
        "iv": iv
      }
      var that = this;
      wx.showLoading({
        title: '加载中...',
      })
      http.wechatBindInfo(data,
        function (res) {
          wx.hideLoading()
          console.log("------用户信息--", res);
          if (res.code == 0) {
            that.wclogin();
          } else if (res.code == 2) {
            wx.showToast({
              title: '绑定微信成功，请绑定手机号',
              icon: "none"
            })
            that.setData({
              type: 2
            })
          }
        },
        function (err) {
          wx.hideLoading()
        })
      console.log("----------", data);
    }
  },

  wclogin: function () {
    var that = this;
    wx.login({ //调用获取用户openId
      success: function (res) {
        that.wxlogin(res.code)
      }
    })
  },

  wxlogin: function (code) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.weChatLogin({
        code: code
      },
      function (res) {
        wx.hideLoading()
        console.log("------用户信息--", res);
        if (res.code == 0) {
          getApp().globalData.isLogin = true;
          getApp().globalData.token = res.token;
          that.getuser();
        }
      },
      function (err) {
        wx.hideLoading()
      })
  },

  //获取用户信息
  getuser: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.userInfo({},
      function (res) {
        wx.hideLoading()
        console.log("------用户信息--", res);
        if (res.code == 0) {
          getApp().globalData.userInfo = res.data;
           wx.reLaunch({
             url: '/pages/index/index',
           })
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    debugger
    if (options.ptype) {
      this.setData({
        ptype: options.ptype,
      })
      if (options.ptype == "work") {
        this.setData({
          id: options.id
        })
      }
    }
    this.setData({
      type: options.type,
    })
    // this.wxlogin()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: '/pages/index/index', // 
      imageUrl: '',
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
        }
      }
    }
    console.log("------------", shareObj);
    // 返回shareObj
    return shareObj;
  }
})